﻿using ServerCommon.Config;
using ServerCommon.Log;
using ServerCommon.Net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WorldServer.Server
{
    public class WorldNet
    {
        public static readonly WorldNet Instance = new Lazy<WorldNet>(() => new WorldNet(), true).Value;
        private static LogHelper log = LogHelper.GetLogger(typeof(WorldNet));

        NetNodeManager _managerWorld;
        NetService _serviceWorld;

        NetNodeManager _managerGM;
        NetClient _clientGM;

        NetNodeManager _managerDatabase;
        NetClient _clientDatabase;

        public void Init()
        {
            _managerWorld = new NetNodeManager();
            _managerWorld.Init();

            _managerGM = new NetNodeManager();
            _managerGM.Init();

            _managerDatabase = new NetNodeManager();
            _managerDatabase.Init();

            var netConfig = ConfigManager.Instance.NetConfig["NetConfigure"];
            var worldNetConfig = netConfig["WorldNetConfig"];
            var serviceNetConfig = worldNetConfig["GameService"];
            int connectionCount = serviceNetConfig.Value<int>("MaxConnection");
            int bufferSize = serviceNetConfig.Value<int>("SAEABufferSize");

            var serverConfig = ConfigManager.Instance.ServerConfig;
            var worldConfig = serverConfig["WorldServer"];
            string ip = worldConfig.Value<string>("Ip");
            int port = worldConfig.Value<int>("Port");
            long localServerId = worldConfig.Value<long>("Id");

            _serviceWorld = new NetService();
            _serviceWorld.Config(_managerWorld, EnumNodeServiceType.Database, ip, port, connectionCount, bufferSize, localServerId);
            _serviceWorld.Start();
            _serviceWorld.ShowPrompt();

            var clientNetConfig = netConfig["ServerCustomConfig"];
            connectionCount = clientNetConfig.Value<int>("MaxConnection");
            bufferSize = clientNetConfig.Value<int>("SAEABufferSize");

            var gmConfig = serverConfig["GMServer"];
            ip = gmConfig.Value<string>("Ip");
            port = gmConfig.Value<int>("Port");
            long gmServerId = gmConfig.Value<long>("Id");
            _clientGM = new NetClient();
            _clientGM.Config(_managerGM, ip, port, connectionCount, bufferSize, gmServerId, localServerId);
            _clientGM.Start();
            _clientGM.ShowPrompt();

            var dbConfig = serverConfig["DatabaseServer"];
            ip = dbConfig.Value<string>("Ip");
            port = dbConfig.Value<int>("Port");
            long dbServerId = dbConfig.Value<long>("Id");
            _clientDatabase = new NetClient();
            _clientDatabase.Config(_managerDatabase, ip, port, connectionCount, bufferSize, dbServerId, localServerId);
            _clientDatabase.Start();
            _clientDatabase.ShowPrompt();

            NetNodeReconnect.Instance.Init();
        }

        public void Run()
        {
            _managerDatabase.DoNetMessages();
            _managerWorld.DoNetMessages();
            _managerGM.DoNetMessages();
        }

        public void Stop()
        {
            NetNodeReconnect.Instance.Release();

            _managerWorld.StopNetwork();
            _managerDatabase.StopNetwork();
            _managerGM.StopNetwork();
        }

        public void Release()
        {
        }
    }
}
